package com.example.android.codelabs.navigation;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import Untils.Code;
import Service.RegistrationService;
import Untils.Keyboard;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn_cancel;//取消
    private EditText et_login_account;//账号
    private EditText et_login_password_ok;//密码
    private EditText et_code;//验证码
    private Button btn_ok;//确定
    private ImageView iv_code;
    private String realCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();

    }
    private void initView() {
        btn_cancel = (Button) findViewById(R.id.btn_cancel);
        btn_cancel.setOnClickListener(this);
        btn_ok = (Button) findViewById(R.id.btn_ok);
        btn_ok.setOnClickListener(this);
        et_login_account = (EditText) findViewById(R.id.et_login_account);
        et_login_password_ok = (EditText) findViewById(R.id.et_login_password_ok);
        et_code = (EditText) findViewById(R.id.et_code_companyRe);
        iv_code = (ImageView) findViewById(R.id.iv_code);
        iv_code.setOnClickListener(this);
        //将验证码用图片的形式显示出来
        iv_code.setImageBitmap(Code.getInstance().createBitmap());
        realCode = Code.getInstance().getCode().toLowerCase();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_code://验证码
                iv_code.setImageBitmap(Code.getInstance().createBitmap());
                realCode = Code.getInstance().getCode().toLowerCase();
                break;
            case R.id.btn_cancel://取消
                RegisterActivity.this.finish();
                break;
            case R.id.btn_ok://确定

                String etCode=et_code.getText().toString().toLowerCase();
                String zhanghao=et_login_account.getText().toString();
                String mima=et_login_password_ok.getText().toString();
                RegistrationService registrationService = new RegistrationService(this);
                Boolean registrationResult = registrationService.registerUser(zhanghao, mima);
                if (registrationResult){
                    Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                    et_login_account.getText().clear();
                    et_login_password_ok.getText().clear();
                    et_code.getText().clear();
                }else {
                    Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
                    et_login_account.getText().clear();
                    et_login_password_ok.getText().clear();
                    et_code.getText().clear();

                }

                break;
        }

    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //获取当前获得焦点的View
                View view = getCurrentFocus();
                //调用方法判断是否需要隐藏键盘
                Keyboard.hideKeyboard(ev, view, this);
                break;
        }
        return super.dispatchTouchEvent(ev);
    }
}
